/*****************************************************************************
** CS221 Computer Vision Project
** Copyright (c) 2006, Stanford University
**
** FILENAME:    KMeansBW.h
** AUTHOR(S):  	Chanh Nguyen <chanhn@stanford.edu>
** 				Divij Gupta <divijg@stanford.edu>
** 				Yongwhan Lim <yongwhan@stanford.edu>
** DESCRIPTION:
** This runs the KMeans BLACK AND WHITE algorithm over an image to find the K clusters.
**

*****************************************************************************/

#pragma once

#include "cv.h"
#include "cxcore.h"

#include "utils.h"
#include "objects.h"
#include "template.h"

typedef struct KPointBW {
	int x;
	int y;
	float value;
};

typedef struct KClusterBW {
	float center;
	float prev_center;
	std::vector<KPointBW> points;
	float random;
} KClusterBW;


/* Classifier class ---------------------------------------------------------
 */
class KMeansBW {
protected:

    int num_clusters;
    std::vector<KClusterBW > clusters;
    const IplImage *image;
	bool converged;

public:
    // constructors
    KMeansBW(const IplImage *image, int num_clusters);

    // destructor
    virtual ~KMeansBW();

    // load and save classifier configuration
    virtual std::vector<KClusterBW> findClusters();
    virtual IplImage* getClusterImage();
    virtual IplImage* getSingleClusterImage(int cluster_num);

private:

    virtual void findAverage(KClusterBW *c);
    virtual void findFarthest(KClusterBW *c);
    virtual float distance(float a, float b);
    virtual int assignCluster(KPointBW &p);

};




